package com.xquant.databasemanagement.bean;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.xquant.databasemanagement.bean.table.TableBean;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

import java.util.ArrayList;
import java.util.List;

@JacksonXmlRootElement(localName = "database")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class DatabaseBean {

    private String databaseName;

    @JacksonXmlProperty(localName = "table")
    @JacksonXmlElementWrapper(localName = "tableList")
    private List<TableBean> tableList = new ArrayList<>();

    @JacksonXmlProperty(localName = "sequence")
    @JacksonXmlElementWrapper(localName = "sequenceList")
    private List<SequenceBean> sequenceList = new ArrayList<>();

    @JacksonXmlProperty(localName = "basicData")
    @JacksonXmlElementWrapper(localName = "basicDataList")
    private List<CommonObject> basicDataList = new ArrayList<>();

    @JacksonXmlProperty(localName = "trigger")
    @JacksonXmlElementWrapper(localName = "triggerList")
    private List<CommonObject> triggerList = new ArrayList<>();

    @JacksonXmlProperty(localName = "function")
    @JacksonXmlElementWrapper(localName = "functionList")
    private List<CommonObject> functionList = new ArrayList<>();

    @JacksonXmlProperty(localName = "procedure")
    @JacksonXmlElementWrapper(localName = "procedureList")
    private List<CommonObject> procedureList = new ArrayList<>();

    @JacksonXmlProperty(localName = "view")
    @JacksonXmlElementWrapper(localName = "viewList")
    private List<CommonObject> viewList = new ArrayList<>();



    @JacksonXmlProperty(isAttribute = true)
    public String getDatabaseName() {
        return databaseName;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }


    public List<TableBean> getTableList() {
        return tableList;
    }


    public void setTableList(List<TableBean> tableList) {
        this.tableList = tableList;
    }


    public List<SequenceBean> getSequenceList() {
        return sequenceList;
    }

    public void setSequenceList(List<SequenceBean> sequenceList) {
        this.sequenceList = sequenceList;
    }



    public List<CommonObject> getBasicDataList() {
        return basicDataList;
    }

    public void setBasicDataList(List<CommonObject> basicDataList) {
        this.basicDataList = basicDataList;
    }



    public List<CommonObject> getTriggerList() {
        return triggerList;
    }

    public void setTriggerList(List<CommonObject> triggerList) {
        this.triggerList = triggerList;
    }

    public List<CommonObject> getFunctionList() {
        return functionList;
    }

    public void setFunctionList(List<CommonObject> functionList) {
        this.functionList = functionList;
    }

    public List<CommonObject> getProcedureList() {
        return procedureList;
    }

    public void setProcedureList(List<CommonObject> procedureList) {
        this.procedureList = procedureList;
    }

    public List<CommonObject> getViewList() {
        return viewList;
    }

    public void setViewList(List<CommonObject> viewList) {
        this.viewList = viewList;
    }


    public void transformTableList(ObservableList<TreeItem> tableList) {
        if (tableList==null||tableList.size()<=0) {
            return;
        }
        this.tableList.clear();
        for (TreeItem t : tableList    ) {
            this.tableList.add((TableBean) t.getValue());
        }
    }

    public void transformSequenceList(ObservableList<TreeItem> sequenceList) {
        if (sequenceList==null||sequenceList.size()<=0) {
            return;
        }
        this.sequenceList.clear();
        for (TreeItem t : sequenceList    ) {
            this.sequenceList.add((SequenceBean) t.getValue());
        }
    }


    public void transformViewList(ObservableList<TreeItem> viewList) {
        if (viewList==null||viewList.size()<=0) {
            return;
        }

        this.viewList.clear();
        for (TreeItem t : viewList    ) {
            this.viewList.add((CommonObject) t.getValue());
        }
    }


    public void transformProcedureList(ObservableList<TreeItem> procedureList) {
        if (procedureList==null||procedureList.size()<=0) {
            return;
        }
        this.procedureList.clear();
        for (TreeItem t : procedureList    ) {
            this.procedureList.add((CommonObject) t.getValue());
        }
    }


    public void transformBasicDataList(ObservableList<TreeItem> basicDataList) {
        if (basicDataList==null||basicDataList.size()<=0) {
            return;
        }
        this.basicDataList.clear();
        for (TreeItem t : basicDataList    ) {
            this.basicDataList.add((CommonObject) t.getValue());
        }
    }


    public void transformFunctionList(ObservableList<TreeItem> functionList) {
        if (functionList==null||functionList.size()<=0) {
            return;
        }
        this.functionList.clear();
        for (TreeItem t : functionList    ) {
            this.functionList.add((CommonObject) t.getValue());
        }
    }


    public void transformTriggerList(ObservableList<TreeItem> triggerList) {
        if (triggerList==null||triggerList.size()<=0) {
            return;
        }
        this.triggerList.clear();
        for (TreeItem t : triggerList    ) {
            this.triggerList.add((CommonObject) t.getValue());
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null)
            return true;
        if (obj instanceof DatabaseBean){
            DatabaseBean databaseModel = (DatabaseBean) obj;
            return databaseModel.getTableList().equals(this.getTableList()) &&
                    databaseModel.getBasicDataList().equals(this.getBasicDataList()) &&
                    databaseModel.getFunctionList().equals(this.getFunctionList()) &&
                    databaseModel.getProcedureList().equals(this.getProcedureList()) &&
                    databaseModel.getTriggerList().equals(this.getTriggerList()) &&
                    databaseModel.getViewList().equals(this.getViewList()) &&
                    databaseModel.getSequenceList().equals(this.getSequenceList());
        }
        return false;
    }

//    private String getOracleSql(){
//
//    }
//
//    private String getMysql(){
//
//    }
}
